/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlythuvien;

import thuvien.Account;
import thuvien.Sach;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import thuvien.NhaXuatBan;
import thuvien.TacGia;
import thuvien.TheLoai;
import thuvien.ThuVienInterface;

/**
 *
 * @author Tuyen
 */
public class ThuVienManager implements ThuVienInterface{

    public ThuVienManager() {
    }

    @Override
    public Account getAccount(String name) throws RemoteException {
        Account ac=new Account();
        try {
            DBConnect db=new DBConnect();
            db.ConnectDB();
            ResultSet rs=db.getAccoutByName(name);
            rs.next();
            ac=new Account(rs.getString(1), rs.getString(2),rs.getInt(3));
            System.out.println(ac.getUsername()+"-"+ac.getPass()+"-"+ac.getMand());
            
        } catch (SQLException ex) {
            Logger.getLogger(ThuVienManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  ac;
    }

    @Override
    public boolean TestAccount(String name) throws RemoteException {
        boolean b =true;
        try {
            DBConnect db=new DBConnect();
            db.ConnectDB();
            ResultSet rs=db.getAccoutByName(name);
            rs.next();
            Account ac=new Account(rs.getString(1), rs.getString(2),rs.getInt(3));
            System.out.println(ac.getUsername()+"-"+ac.getPass()+"-"+ac.getMand());
        } catch (SQLException ex) {
           b=false;
        }
           return b;
    }

    @Override
    public void addAccount(String name, String pass, int sdt) throws RemoteException {
            DBConnect db=new DBConnect();
            db.ConnectDB();
            db.addNewAccount(name, pass, sdt);
        }
    @Override
    public void addDocGia(String name, int GioiTinh, String quequan, int sdt, String email) throws RemoteException {
        DBConnect db=new DBConnect();
        db.ConnectDB();
        db.addNewDocGia(name, GioiTinh, quequan, sdt, email);
     }

    @Override
    public ArrayList<Sach> getAllSach() throws RemoteException {
        DBConnect db=new DBConnect();
        db.ConnectDB();
        ResultSet rs=db.getAllSach();
        ArrayList<Sach> arr=new ArrayList<>();
        try {
            while(rs.next()){
                try {
                    Sach sach=new Sach(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5), rs.getInt(6), rs.getInt(7));
                    arr.add(sach);
                } catch (SQLException ex) {
                    Logger.getLogger(ThuVienManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ThuVienManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return arr;
    }

    @Override
    public NhaXuatBan getNXBByID(int id) throws RemoteException {
         DBConnect db=new DBConnect();
        db.ConnectDB();
        NhaXuatBan nxb=new NhaXuatBan();
        try {
            nxb=db.getNXBByID(id);
        } catch (SQLException ex) {
            Logger.getLogger(ThuVienManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nxb;
    }

    @Override
    public TacGia getTacGiaByID(int id) throws RemoteException {
         DBConnect db=new DBConnect();
        db.ConnectDB();
        TacGia tg=new TacGia();
        try {
            tg=db.getTacGiaByID(id);
        } catch (SQLException ex) {
            Logger.getLogger(ThuVienManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tg;
         }

    @Override
    public TheLoai getTheLoaiByID(int id) throws RemoteException {
         DBConnect db=new DBConnect();
        db.ConnectDB();
        TheLoai tl=new TheLoai();
        try {
            tl = db.getTheLoaiByID(id);
        } catch (SQLException ex) {
            Logger.getLogger(ThuVienManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tl;
    }

 
}
